package com.formssi.multipledatasources.controller;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.formssi.multipledatasources.entity.Emp;
import com.formssi.multipledatasources.test1.EmpMapper1;
import com.formssi.multipledatasources.test2.EmpMapper2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
//@Api("测试多数据源接口")
public class TestController {
    @Autowired
    private EmpMapper1 empMapper1;
    @Autowired
    private EmpMapper2 empMapper2;

//    @ApiOperation("测试mybatis@select注解，通过test1数据库实现")
    @GetMapping("/getKing1")
    public List getKing1(){
        List<Emp> emps = empMapper1.selectList();
        return emps;
    }


//    @ApiOperation("测试mybatis@select注解，通过test2数据库实现")
    @GetMapping("/getKing2")
    public List getKing2(){
        List<Emp> emps = empMapper2.selectList();
        return emps;
    }

//    @ApiOperation("测试mybatis的mapper.xml文件调用，通过test1数据库实现")
    @GetMapping("/getKing3")
    public List getKing3(){
        List<Emp> emps = empMapper1.getAll();
        return emps;
    }

//    @ApiOperation("测试mybatis的mapper.xml文件调用，通过test2数据库实现")
    @GetMapping("/getKing4")
    public List getKing4(){
        List<Emp> emps = empMapper2.getAll();
        return emps;
    }

//    @ApiOperation("通过mp调用test1数据库实现查询")
    @GetMapping("/getKing5")
    public List getKing5(){
        List<Emp> emps = empMapper1.selectList(null);
        return emps;
    }

//    @ApiOperation("通过mp调用test2数据库实现查询")
    @GetMapping("/getKing6")
    public List getKing6(){
        List<Emp> emps = empMapper2.selectList(null);
        return emps;
    }

//    @ApiOperation("测试插入数据")
    @PostMapping("/saveEmp1")
    @Transactional(value = "test1TransactionManager")
    public String saveEmp(Emp emp) {
        int insert = empMapper1.insert(emp);
        if(insert > 0){
            return "插入成功";
        }else{
            return "插入失败";
        }
    }

}
